import {Component, OnInit} from '@angular/core';
import {Shop} from "../../../store/shop/shop.actions";
import {Select, Store} from "@ngxs/store";
import {Observable} from "rxjs";
import {ShopModel} from "../../../store/shop/shop.state";
import {Cart} from "../../../store/cart/cart.actions";
import {CartModel} from "../../../store/cart/cart.state";

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {
  list: Shop[] = []
  @Select() shop$: Observable<ShopModel>
  carts: Shop[] = []
  @Select() cart$: Observable<CartModel>

  constructor(private store: Store) {
  }

  ngOnInit(): void {
    this.store.dispatch(Shop.Get)
    this.shop$.subscribe(r => {
      this.list = JSON.parse(JSON.stringify(r.list))
    })
    this.cart$.subscribe(r => {
      this.carts = r.list
    })
  }

  addToCart(item: Shop) {
    this.store.dispatch(new Cart.Add(item))
  }
}
